<template>

  <Card class="ll cpyszjy-page page-fill" style="height:900px">
    <div>产品映射资金源</div>
    <div id="middle">
      <v-form>
        <v-form-item label="产品大类:" labelWidth="200px">
          <v-select v-model="productType" :select-list="productTypeList" @item-click="productTypeCheck"></v-select>
        </v-form-item>
        <v-form-item label="版本:" labelWidth="200px">
          <v-input class="" v-model="version" placeholder="版本" disabled></v-input>
        </v-form-item>
        <v-form-item label="资金源:" labelWidth="200px">
          <v-select v-model="fund" :select-list="fundList"></v-select>
        </v-form-item>
        <v-form-item label="状态:" labelWidth="200px">
          <v-select v-model="status" :select-list="statusList"></v-select>
        </v-form-item>

        <v-form-item label="优先级:" labelWidth="200px">
          <v-input class="" v-model="prior" placeholder="优先级"></v-input>
        </v-form-item>
        <v-form-item label="资金年化利率:" labelWidth="200px">
          <v-input v-model="rate" placeholder="资金年化利率"></v-input>
        </v-form-item>
        <v-form-item label="产品细类:" labelWidth="200px">
          <v-checkbox-group v-model="productValueList" class="check">
            <v-checkbox v-for="item in productList" :key="item.productCode" :label='item.productCode+":"+item.productName'>{{item.productName}}</v-checkbox>

          </v-checkbox-group>
          <v-checkbox v-if="productList.length>0" @on-change="chooseAll" v-model="mark">全选</v-checkbox>
        </v-form-item>
        <v-button class="bt" size="normal" type="primary" @on-click="conf">确认</v-button>
        <v-button size="normal" class="bt" type="primary" @on-click="reback()">返回</v-button>
      </v-form>
    </div>
  </Card>
</template>

<script>

export default {
  data () {
    return {
      mark: '',
      productType: '',
      productTypeList: [],
      version: '',
      fund: '',
      fundList: [],
      status: '',
      statusList: [
        {
          value: '0',
          label: '正常'
        },
        {
          value: '1',
          label: '异常'
        }
      ],
      prior: '',
      rate: '',
      productValueList: [],
      productList: []
    }
  },
  created () {
    var vm = this
    this.$ajax = this.$util.ajax(this.$appContext.marvels_core_product)
    // 产品大类查询
    this.$ajax.post('product/mcpt/service/searchProduct', {}).then(re => {
      var allData = re.data.data
      for (var i = 0; i < allData.length; i++) {
        vm.productTypeList.push({
          value: allData[i].productType + '-' + allData[i].productTypeName,
          label: allData[i].productTypeName
        })
      }
    })

    // 获取所有资金源
    this.$ajax.post('financial/fund/service/list', {})
      .then(re => {
        var allData = re.data.data.list
        for (var i = 0; i < allData.length; i++) {
          vm.fundList.push({
            value: allData[i].fundCode + '-' + allData[i].fundName,
            label: allData[i].fundName
          })
        }
      })
      .catch(er => { })
  },
  methods: {
    reback () {
      var vm = this
      vm.$router.push('productFund')
    },
    productTypeCheck () {
      var vm = this
      // 查询生效版本
      vm.version = ''
      this.$ajax.post('product/mcpt/service/searchProductVersion', {
        productType: vm.productType.split('-')[0],
        isValid: '0'
      })
        .then(re => {
          vm.version = re.data.data[0].productVersion
          this.$ajax.post('product/mcpt/service/searchByVersion', {
            productType: vm.productType.split('-')[0],
            productVersion: vm.version
          })
            .then(re => {
              vm.productList = re.data.data
            })
            .catch(er => { })
        })
        .catch(er => { })
    },
    chooseAll (param) {
      var vm = this
      if (vm.mark === '') {
        vm.productValueList = []
        for (var i = 0; i < vm.productList.length; i++) {
          vm.productValueList.push(
            vm.productList[i].productCode + ':' + vm.productList[i].productName
          )
        }
        vm.mark = '1'
      } else if (vm.mark === '1') {
        vm.productValueList = []
      }
    },
    conf () {
      var vm = this
      if (vm.productType.trim() === '') {
        this.$alert({ title: '提示', content: '请选择产品大类' })
        return
      }
      if (vm.version.trim() === '') {
        this.$alert({ title: '提示', content: '版本不能为空' })
        return
      }
      if (vm.fund === '') {
        this.$alert({ title: '提示', content: '请选择资金源' })
        return
      }
      if (vm.status.trim() === '') {
        this.$alert({ title: '提示', content: '请选择状态' })
        return
      }
      if (vm.prior.trim() === '') {
        this.$alert({ title: '提示', content: '请输入优先级' })
        return
      }
      if (vm.rate.trim() === '') {
        this.$alert({ title: '提示', content: '请输入年利率' })
        return
      }
      if (vm.productValueList.length === 0) {
        this.$alert({ title: '提示', content: '请选择产品细类' })
        return
      }

      this.$ajax.post('product/mcpt/service/saveProductAndFund', {
        productType: vm.productType,
        productVersion: vm.version,
        fundCode: vm.fund,
        status: vm.status,
        level: vm.prior,
        rate: vm.rate,
        productValueList: vm.productValueList
      })
        .then(re => {
          if (re.data.code === 'success') {
            this.$alert({ title: '提示', content: re.data.data })
            this.$router.push('cpyszjy')
          } else {
            this.$alert({ title: '提示', content: re.data.data })
          }
        })
        .catch(er => { })
    }
  }
}
</script>

<style lang="less">
.cpyszjy-page {
  .ll {
  }
  #middle {
    margin: 100px auto;
    width: 500px;
  }
  #middle .v-form-label {
    font-size: 15px;
  }
  #middle .versionNum {
    width: 80px;
  }
  #middle .channel {
    font-size: 12px;
  }
  .bt {
    margin: 10px 71px;
  }
  .check {
    display: inline-block;
  }
  .termRange {
    margin-right: 20px;
  }
  .v-tag {
    padding: 6px 10px;
    min-width: 10px;
  }
  #tagContainer {
    margin-top: 10px;
  }
  .middiv {
    display: inline-block;
    width: 118px;
  }
  .v-checkbox {
    margin: 15px 20px 6px 15px;
    display: inline-block;
  }
}
</style>
